import { defineStore } from 'pinia';
import request from '@/api/index';
import { LocationQueryValue } from 'vue-router';
import { xmloading } from '@/utils/public';
export const userStore = defineStore('user', {
  state: () => ({
    user_id:'' as LocationQueryValue | LocationQueryValue[],
    u_openid:'' as LocationQueryValue | LocationQueryValue[],
    rewardList:[] as Array<any>,
    eventRewards:0,
    activeStatus:0, // 1 活动未开始 2活动已结束
  }),
  getters: {
    
  },
  actions: {
    async getSystemInfo() {
      const res = await request.getSystemInfo()
      console.log(res.data.now_time);
      const { now_time=0 } = res?.data || {}
      return now_time
    },
    getTime(time:string){
      return Number((new Date(time.replaceAll('-','/')).getTime() / 1000).toFixed(0))
    },
    async getActInfo() {
      const loading = xmloading()
      const now_time = await this.getSystemInfo()
      const res = await request.getUserInfo()
      loading.close()
      const { reward_2=[], reward_1=0, start_time=0, end_time=0 } = res?.data || {}
      // console.log('start_time', this.getTime(start_time));
      // console.log('end_time', this.getTime(end_time));
      if(now_time < this.getTime(start_time)) this.activeStatus = 1
      if(now_time > this.getTime(end_time)) this.activeStatus = 2

      this.eventRewards = reward_1
      const list = [
        {key:1,name:'笨笨龟'},
        {key:2,name:'开心狗'},
        {key:3,name:'快乐羊'},
        {key:4,name:'调皮狐'},
        {key:5,name:'顽皮鹿'},
        {key:6,name:'爱心猫'},
        {key:7,name:'彩虹兽'},
      ]
      this.rewardList = reward_2.map((item:any)=>{
        const { poolId } = item
        return {
          ...item,
          name:list.find(e=>e.key == poolId)?.name
        }
      })
    }
  },
})